package com.atguigu.category.controller;

import com.alibaba.druid.util.StringUtils;
import com.atguigu.category.service.CategoryService;
import com.atguigu.param.ProductHotParam;
import com.atguigu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * projectName: b2c-store
 * description: 类别控制器类
 */

@RestController
@RequestMapping("category")

public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分类商品查询
     * @param categoryName
     * @return
     */

    @GetMapping("/promo/{categoryName}")
    public R byName(@PathVariable String categoryName) {
        if (StringUtils.isEmpty(categoryName)) {
            return R.fail("类别名称为null，无法查询！");
        }

        return categoryService.byName(categoryName);

    }

    /**
     * 热门类别id查询
     * @param productHotParam
     * @param result
     * @return
     */

    @PostMapping("hots")
    public R hotCategory(@RequestBody @Validated ProductHotParam productHotParam, BindingResult result) {
        if (result.hasErrors()){
            return R.fail("类别集合查询失败!");
        }

        return categoryService.hotsCategory(productHotParam);
    }


    /**
     * 类别商品信息查询
     * @return
     */

    @GetMapping("list")
    public R list() {

        return categoryService.list();
    }

}
